package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
    private CartDao dao = new CartDaoImpl();
    @Override
    public List<Cart> listCart(Object... item) {
        List<Cart> list = new ArrayList<>();
        try {
            list = dao.listCart(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Integer saveCart(String userId, Integer businessId, Integer foodId,Integer quantity) {
        int result = 0;
        try {
            result = dao.saveCart(userId, businessId, foodId,quantity);
            DBUtil.beginTransaction();
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer updateCart(Cart cart) {
        int result=0;
        try {
            result= dao.updateCart(cart);
            DBUtil.beginTransaction();
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer removeCart(Object... item) {
       int result=0;
        try {
            result = dao.removeCart(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
